import { createRouter, createWebHashHistory } from 'vue-router'
import { TokenUtil, StorageUtil } from '@/utils/common.js'
import { useMenuStore } from "@/stores/menu";
import { useUserStore } from "@/stores/user.js";
const routes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/user/Login.vue'),
    meta: {
      title: '登录',
      requiresAuth: false
    }
  },
  {
    path: '/register',
    name: 'register',
    component: () => import('@/views/user/register.vue'),
    meta: {
      title: '注册',
      requiresAuth: false
    }
  },
  {
    path: '/forgot-password',
    name: 'forgot-password',
    component: () => import('@/views/user/forgot-password.vue'),
    meta: {
      title: '重置密码',
      requiresAuth: false
    }
  },
  {
    path: '/assessment-details/:testId',
    name: 'assessment-details',
    component: () => import('@/views/Assessment-details.vue'),
    meta: {
      title: '考核详情',
      requiresAuth: false
    }
  },
  {
    path: '/',
    name: 'Home',
    redirect: to => ({
      name: 'home-assessment',
      params: { tab: '1' }
    }),
    component: () => import('@/views/Home.vue'),
    meta: {
      requiresAuth: true
    },
    children: [
      {
        path: '/home-assessment/:tab',
        name: 'home-assessment',
        props: true,
        component: () => import('@/views/home/index.vue')
      },
      {
        path: '/badge',
        name: 'badge',
        component: () => import('@/views/badge/index.vue'),
        meta: {
          type: '6'
        }
      },
      {
        path: '/version',
        name: 'Version',
        component: () => import('@/views/user/version.vue'),
        meta: {
          type: '3'
        }
      },
      {
        path: '/teacherAssessList',
        name: 'TeacherAssessList',
        component: () => import('@/views/examCourses/teacherAssessList.vue'),
        meta: {
          type: '4'
        }
      },
      {
        path: '/associationAssessList',
        name: 'AssociationAssessList',
        component: () => import('@/views/examCourses/teacherAssessList.vue'),
        meta: {
          type: '5'
        }
      },
      {
        path: '/examCourses',
        name: 'ExamCourses',
        component: () => import('@/views/examCourses/index.vue'),
        meta: {
          type: '2'
        },
        children: [
          {
            path: '/examCourses/post',
            name: 'ExamCoursesPost',
            component: () => import('@/views/examCourses/post.vue'),
            meta: {
              type: '2'
            }
          },
          {
            path: '/examCourses/assessList',
            name: 'ExamCoursesAssessList',
            component: () => import('@/views/examCourses/assessList.vue'),
            meta: {
              type: '2'
            }
          },
          {
            path: '/examCourses/assessDetail',
            name: 'ExamCoursesAssessDetail',
            component: () => import('@/views/examCourses/assessDetail.vue'),
            meta: {
              type: '2'
            }
          },
          {
            path: '/examCourses/chapter',
            name: 'ExamCoursesChapter',
            component: () => import('@/views/examCourses/chapter.vue'),
            meta: {
              type: '2'
            }
          },
          {
            path: '/examCourses/chapterDetail',
            name: 'ExamCoursesChapterDetail',
            component: () => import('@/views/examCourses/chapterDetail2.vue'),
            meta: {
              type: '2'
            }
          },
          {
            path: '/examCourses/assesseEnd',
            name: 'ExamCoursesAssesseEnd',
            component: () => import('@/views/examCourses/assesseEnd.vue'),
            meta: {
              type: '2'
            }
          },
          {
            path: '/examCourses/successfulLevel',
            name: 'SuccessfulLevel',
            component: () => import('@/views/examCourses/successfulLevel.vue'),
            meta: {
              type: '2'
            }
          },
          {
            path: '/examCourses/failfullLevel',
            name: 'FailfullLevel',
            component: () => import('@/views/examCourses/failfullLevel.vue'),
            meta: {
              type: '2'
            }
          },
          {
            path: '/examCourses/courseDetail/:id',
            name: 'courseDetail',
            component: () => import('@/views/examCourses/courseDetail.vue'),
            meta: {
              type: '2'
            }
          },
        ]
      },
    ]
  },
  {
    path: '/conversation',
    name: 'conversation',
    component: () => import('@/views/conversation/index.vue'),
  },
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    component: () => import('@/views/404.vue')
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

// 全局前置守卫
router.beforeEach((to, from, next) => {
  const token = TokenUtil.getToken()
  if (to.meta.requiresAuth && !token) {
    next({ path: '/login' })
  } else if (to.path === '/login' && token) {
    next('/')
  }else {
    const routerType = to.meta.type
    const menuStore = useMenuStore()
    if (routerType == '2') {
      menuStore.setActiveIndex('7');
      menuStore.setMenuType('2');
    } else if (routerType == '4') {
      menuStore.setActiveIndex('9');
      menuStore.setMenuType('4');
    } else if (routerType == '5') {
      menuStore.setActiveIndex('10');
      menuStore.setMenuType('5');
    } else if (routerType == '6') {

    } else {
      menuStore.setActiveIndex('1');
      menuStore.setMenuType('1');
    }
    next()
  }
})

export default router
